package com.ican.javadoc.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.ican.javadoc.common.common.BusinessException;

public class NetConnection {
    
    //发起get请求
    public static String httpGet(String urlStr) throws BusinessException{
        HttpURLConnection httpConn = null;
        try {
            URL url = new URL(urlStr);
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestMethod("GET");
            httpConn.setDoOutput(true);
            return dealConnectIn(httpConn);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }
    
    //发起post请求
    public static String httpPost(String urlStr, String params) throws BusinessException{
        HttpURLConnection httpConn = null;
        try {
            URL url = new URL(urlStr);
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.getOutputStream().write(params.getBytes());
            httpConn.getOutputStream().flush();
            return dealConnectIn(httpConn);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }
    
    //把返回结果转换成字符串
    public static String dealConnectIn(HttpURLConnection httpConn) throws BusinessException{
        InputStream in = null;
        String readLine = null;
        StringBuffer sb = new StringBuffer();
        String charset = null;;
        try {
            //取页面编码，以防出现乱码
            charset = httpConn.getContentType();
            if(charset != null && charset.indexOf("charset=") != -1){
                charset = charset.substring(charset.indexOf("charset=") + 8);
            }else{
                charset = "UTF-8";
            }
            //取得输出流
            in = httpConn.getInputStream();
            InputStreamReader inReader = new InputStreamReader(in,charset);
            BufferedReader reader = new BufferedReader(inReader);
            while((readLine = reader.readLine()) != null) {
                sb.append(readLine);
            }
            return sb.toString();
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        } finally {//关闭流和连接
            if(in != null){
                try {
                    in.close();
                } catch (Exception ex) {}
            }
            if(httpConn != null){
                try {
                    httpConn.disconnect();
                } catch (Exception ex) {}
            }
        }
    }
    
//    public static void main(String[] args){
//      String t = "http://www.baidu.com/s?bs=spring+concurrent&f=8&rsv_bp=1&wd=java++%D7%A5%C8%A1%CD%F8%D2%B3%C4%DA%C8%DD&inputT=14831";
//      logger.info(PageContent.httpGet(t));
//    }
}
